package com.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.*;
import java.util.Date;


public class ExcelUtil {


    public void readExcel(String PATH) throws IOException {
        // 获取文件流
        FileInputStream fileInputStream = new FileInputStream(PATH);
        // 创建一个工作簿，使用excel能操作的这边他都可以操作！
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        // 获取标题内容
        Row rowTitle = sheet.getRow(0);
        if (rowTitle != null) {
            // 一定要掌握
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if (cell != null) {
                    CellType cellType = cell.getCellType();
                    String cellValue = cell.getStringCellValue();
                    System.out.println(cellValue + "|");
                }
            }
        }
        // 获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if (rowData != null) {
                // 读取列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.println("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
                    Cell cell = rowData.getCell(cellNum);
                    // 匹配列的数据类型
                    if (cell != null) {
                        CellType cellType = cell.getCellType();
                        String cellValue = "";
                        switch (cellType) {
                            case _NONE:
                                System.out.println("[None]");
                                break;
                            case STRING:
                                System.out.println("[String]");
                                cellValue = cell.getStringCellValue();
                                break;
                            case BOOLEAN:
                                System.out.println("[Boolean]");
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                            case BLANK:
                                System.out.println("[Blank]");
                                break;
                            case NUMERIC:
                                System.out.println("[NUMERIC]");
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    System.out.println("[日期]");
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                } else {
                                    System.out.println("[转换为字符串输出]");
                                    cellValue = String.valueOf(cell.getNumericCellValue());
                                }
                                break;
                            case ERROR:
                                System.out.println("[ERROR]");
                                break;
                        }
                    }
                }
            }
        }
        fileInputStream.close();
    }

    public static void main(String[] args) {
       String path = "D:\\DownLoad\\DingDingDownLoad\\OA表单设计-修订版-谢军奇-20210125(1).xlsx";
        try {
            new ExcelUtil().testCellType(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
